﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Media.Animation;

using System.Runtime.InteropServices;
using MySql.Data.MySqlClient;

//using WiTeach.ClassManagement.Other;
using WiTeach.Tools;
using WiTeach.Common;

using System.Windows.Threading;
using WiTeach.LoginManagement.DAL;
using WiTeach.CourseManagement.View;
using WiTeach.ClassManagement.DAL;

namespace WiTeach.LoginManagement.View
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Page
    {
        public Login()
        {
            InitializeComponent();

            // 获取登录图片和背景图片
            // 获取通用目录
            string sql = "select value  from ta_conf where name='file_path'";
            string filePath = (string)SqlHelper.ExecuteScalar(sql);

            string sql2 = "select src  from ta_themes where type=2 and status =1";
            string loginImage = (string)SqlHelper.ExecuteScalar(sql2);

            string sql3 = "select src  from ta_themes where type=1 and status =1";
            string bgImage = (string)SqlHelper.ExecuteScalar(sql3);

            string loginImagePath = filePath +'\\'+ loginImage;
            string bgImagePath = filePath + '\\' + bgImage;
            App.backgroundImage = bgImagePath;
            App.loginImge = loginImagePath;
            App.rootPath = filePath;

            // 修改页面背景主题
            ImageBrush brush = new ImageBrush();
            brush.Stretch = Stretch.Fill;
            brush.ImageSource = new BitmapImage(new Uri( App.loginImge, UriKind.RelativeOrAbsolute));
            this.Background = brush;

        }

        //检测粘贴
        private void id_textBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                //if (!isNumberic(text))
                //{ e.CancelCommand(); }
            }
            else { e.CancelCommand(); }
        }



        //private void id_textBox_PreviewKeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Space)
        //        e.Handled = true;
        //}



        private void id_textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //if (!isNumberic(e.Text))
            //{
            //    e.Handled = true;
            //}
            //else
            //    e.Handled = false;
        }


        //isDigit是否是数字
        public static bool isNumberic(string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            foreach (char c in _string)
            {
                if (!char.IsDigit(c))
                    //if(c<'0' c="">'9')//最好的方法,在下面测试数据中再加一个0，然后这种方法效率会搞10毫秒左右
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void login_click(object sender, RoutedEventArgs e)
        {

            string userName = this.id_texbox.Text.ToString();
            string password = this.pw_passwordbox.Password.ToString();
            if (userName.Length == 0 || password.Length == 0)
            {
                System.Windows.MessageBox.Show("用户名或密码不能为空！", "登录提示");
                return;
            }
            else
            {
                int count = UserDAL.getUserCountByName(userName);
                if (count <= 0)
                {
                    System.Windows.MessageBox.Show("未找到用户！", "登录提示");
                    return;
                }
                else
                {
                    string md5pwd = UserDAL.mduser(password);
                    bool flag = UserDAL.checkUser(userName, md5pwd);
                    if (!flag) {
                        System.Windows.MessageBox.Show("用户名或密码错误请重新登录！", "登录提示");
                        return;
                    }
                    else
                    {
                        App.userName = userName;
                        CourseIndex courseIndexPage = new CourseIndex();
                        this.NavigationService.Navigate(courseIndexPage);
                    }
                   
                }
            }
           

        }

        private void shutdown_Click(object sender, RoutedEventArgs e)
        {
            DialogResult dr = (System.Windows.Forms.DialogResult)System.Windows.MessageBox.Show("是否关机","关机对话框",System.Windows.MessageBoxButton.OKCancel,System.Windows.MessageBoxImage.Question);
            if (dr == DialogResult.OK)
            {
                 //点确定的代码
                Process.Start("Shutdown.exe", " -s -t 0");
            }            
        }

        private void restart_Click(object sender, RoutedEventArgs e)
        {
            
            DialogResult dr = (System.Windows.Forms.DialogResult)System.Windows.MessageBox.Show("是否重启", "重启对话框", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question);
            if (dr == DialogResult.OK)
            {
                //点确定的代码
                Process.Start("shutdown.exe", " -r -t 0");
            }    
        }

        private void waitting_Click(object sender, RoutedEventArgs e)
        {
            DialogResult dr = (System.Windows.Forms.DialogResult)System.Windows.MessageBox.Show("是否待机", "待机对话框", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question);
            if (dr == DialogResult.OK)
            {
                //点确定的代码
                System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend, true, false);
            }
            
        }

        private void visitor_Click(object sender, RoutedEventArgs e)
        {
            //FileHelper fileHelper = new FileHelper();
            //string media_path = fileHelper.GetFinderFileFullPath("\\Resource\\video\\", "test.mp4");
            //MediaPlay mp = new MediaPlay(media_path);
            //this.NavigationService.Navigate(mp);
            string sql3 = "select src  from ta_themes where type=3 and status =1";
            string src = (string)SqlHelper.ExecuteScalar(sql3);
            string videoPath = App.rootPath + '\\' + src;

            MediaPlay mp = new MediaPlay(videoPath);
            this.NavigationService.Navigate(mp);

            //NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/Common/pluge/MediaPlay.xaml", UriKind.Relative));
        }

        private void visitor_mouse_enter (object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 75;
            image.Width = 61;
        }
        private void visitor_mouse_leave (object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 65;
            image.Width = 47;
        }

      

   /*     private void login_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            login_click(sender, e);
        }*/

        //         protected override void OnPreviewKeyDown(KeyEventArgs e)
        // 2         {
        // 3             if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) ||
        // 4                 (e.Key >= Key.D0 && e.Key <= Key.D9) ||
        // 5                 e.Key == Key.Back||
        // 6                 e.Key==Key.Left||e.Key==Key.Right)
        // 7             {
        // 8                 if (e.KeyboardDevice.Modifiers != ModifierKeys.None)
        // 9                 {
        //10                     e.Handled = true;
        //11                 }
        //12             }
        //13             else
        //14             {
        //15                 e.Handled = true;
        //16             }
        //17         }
        }
}
